#!/usr/bin/env bash

set -xeo pipefail
#set -x：启用详细模式，使 Bash 在执行每个命令之前打印该命令。这对于调试非常有用。
#set -e：如果任何命令失败（即返回非零退出状态），则使脚本立即退出。这对于确保脚本在出现问题时停止很有用。
#set -u：将未设置的变量视为错误并立即退出。这对于捕捉变量名拼写错误很有用。
#set -o pipefail：使管道在任何命令失败时返回非零退出状态，而不仅仅是最后一个命令。这对于检测涉及多个命令的管道中的错误很有用。

RUN_PATH=$(
  cd "$(dirname $0)"
  pwd
)
cd ${RUN_PATH}
pwd

REPO_GITHUB='https://github.com'
REPO_GITCODE='https://gitcode.net/mirrors'
REPO=${REPO_GITHUB}
chmod +x -- *.sh

function install_oh_my_zsh() {
  sudo apt install zsh -y
  sh -c "$(proxychains4 curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
#  sh -c "$(curl -fsSL https://gitcode.net/mirrors/ohmyzsh/ohmyzsh/-/raw/master/tools/install.sh | sed "s|${REPO_GITHUB}|${REPO_GITCODE}|g")"
  # 卸载命令
  #sh -c "$(curl -fsSL https://gitcode.net/mirrors/ohmyzsh/ohmyzsh/-/raw/master/tools/uninstall.sh)"

  ZSH_PLUGIN_INSTALL_DIR=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins

  git clone --depth=1 ${REPO}/wting/autojump.git ${ZSH_PLUGIN_INSTALL_DIR}/autojump
  cd ${ZSH_PLUGIN_INSTALL_DIR}/autojump
  ./install.py
  cd -

  git clone --depth=1 ${REPO}/zsh-users/zsh-completions ${ZSH_PLUGIN_INSTALL_DIR}/zsh-completions
  git clone --depth=1 ${REPO}/zsh-users/zsh-autosuggestions.git ${ZSH_PLUGIN_INSTALL_DIR}/zsh-autosuggestions
  git clone --depth=1 ${REPO}/zsh-users/zsh-syntax-highlighting.git ${ZSH_PLUGIN_INSTALL_DIR}/zsh-syntax-highlighting

  if [ -e ${HOME}/.zshrc ]; then
    mv ${HOME}/.zshrc ${HOME}/.zshrc.preinstall
  fi
  ln -s ${PWD}/zsh/.zshrc ${HOME}/.zshrc
}

function install_neovim() {
  sudo apt remove vim vim-tiny vim-basic vim-gtk3 -y --allow-change-held-packages
  cd ~
  curl -LO "https://github.com/neovim/neovim/releases/download/stable/nvim-linux64.tar.gz"
  tar -zxvf nvim-linux64.tar.gz
  sudo ln -s ${PWD}/nvim-linux64/bin/nvim /usr/local/bin/nvim
  sudo ln -s /usr/local/bin/nvim /usr/local/bin/vim
  cd -
  if [ -e ${HOME}/.config/nvim/ ]; then
    mv ${HOME}/.config/nvim/ ${HOME}/.config/nvim-preinstall/
  fi
  ln -s ${PWD}/nvim/ ${HOME}/.config/nvim/
}
function main() {
  install_oh_my_zsh
#  install_neovim
}

main
